package com.iman.manjusri.domain;

import java.io.Serializable;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;


public class MessageDetailBean extends BaseMessageDetailBean implements Serializable{
	
	
	
	public enum UpdateStatusEnum{
		/**准备更新*/
		PREPARE(1),
		/**更新中*/
		UPDATING(2),
		/**已更新*/
		UPDATED(3),
		/**更新失败*/
		FAILED(4);
		
		private int value;
		
		private UpdateStatusEnum(int value) {
			this.value = value;
		}

		public int getValue() {
			return value;
		}
		
	}

	public MessageDetailBean() {
		super();
		init();
	}
	
	

	public void setUpdateStatus(UpdateStatusEnum status){
		if(status!=null){
			setUpdateStatus(status.getValue());
		}
	}
	
	public UpdateStatusEnum getUpdateStatusEnum(){
		if(getUpdateStatus()==UpdateStatusEnum.PREPARE.getValue()){
			return UpdateStatusEnum.PREPARE;
		}
		if(getUpdateStatus()==UpdateStatusEnum.UPDATING.getValue()){
			return UpdateStatusEnum.UPDATING;
		}
		if(getUpdateStatus()==UpdateStatusEnum.UPDATED.getValue()){
			return UpdateStatusEnum.UPDATED;
		}
		if(getUpdateStatus()==UpdateStatusEnum.FAILED.getValue()){
			return UpdateStatusEnum.FAILED;
		}
		return null;
	}
	
	public void init(){
		if(getUpdateStatus()==0){
			setUpdateStatus(UpdateStatusEnum.PREPARE);
		}
	}
	
	public MessageDetailBean jsonToBean(String json){
		if(json==null||"".equals(json)){
			return null;
		}
		JSONObject idJson = JSON.parseObject(json);
		if(idJson!=null){
			String oid = JSON.parseObject(idJson.getString(ID)).getString("$oid");
			setId(oid);
		}
		JSONObject detailContentJson = JSON.parseObject(json);
		if(detailContentJson!=null){
			setDetailContent(detailContentJson.getString(DETAIL_CONTENT));
		}
		JSONObject updateStatusJson = JSON.parseObject(json);
		if(updateStatusJson!=null){
			setUpdateStatus(updateStatusJson.getInteger(UPDATE_STATUS));
		}
		JSONObject sellerIdJson = JSON.parseObject(json);
		if(sellerIdJson!=null){
			setSellerId(sellerIdJson.getInteger(SELLER_ID));
		}
		JSONObject updateCountJson = JSON.parseObject(json);
		if(updateCountJson!=null){
			setUpdateCount(updateCountJson.getInteger(UPDATE_COUNT));
		}
		return this;
	}
}
